<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
class Play extends Model{
    protected $insert = ['play_js'];
    protected function setPlayJsAttr($value,$data){
		if($value){
			$copy=copy(ROOT_PATH."public/player/zanpiancms_".$value.".js",ROOT_PATH.'public/player/'.$data['play_name'].".js");
		}
    }
    protected function setPlayCodeAttr($value,$data){
		if(empty($data['play_js']) && $value){
		    $res = fwrite(fopen(ROOT_PATH.'public/player/' . $data['play_name'].'.js','wb'),$value);
            if($res===false){
                return $this->error('保存代码文件失败，请重试!');
            }
		}
    }
    public function play_cache(){
	       $play = db('play')->order('play_oid asc')->column('*','play_name');
           F('_play/play',$play);
		   $play = db('play')->where('play_status',1)->order('play_oid asc')->column('*','play_name');
           F('_play/play_status',$play);	   
	}
	//事件注册
    protected static function init(){
		//删除后置事件
        Play::afterDelete(function ($play){
			    //删除播放器JS
                @unlink(ROOT_PATH.'public/player/'.$play->play_name.'.js');
       });	
    }	
	//过滤不存在的播放组
    public function filter_play_array($play,$url){
		if(!empty($play) && !empty($url)){
		    $playarray = is_array($play)?$play:explode('$$$',$play);
		    $urlarray = is_array($url)?$url:explode('$$$',$url);			
		    $status=F('_play/play_status');
		    foreach($playarray as $key=>$value){
			    if(empty($status[$value])){
				    unset($playarray[$key]);
				    unset($urlarray[$key]);				
			    }
		    }
			$data['vod_play']=implode('$$$',$playarray);
			$data['vod_url']=implode('$$$',$urlarray);
            return 	$data;
		}else{
			return false;
		}
	}	
	
	
}